/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.medicine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oyun.modules.medicine.domain.AdministrativeOffice;
import com.oyun.modules.medicine.domain.Doctor;
import com.oyun.modules.medicine.domain.HospitalizedCase;
import com.oyun.modules.medicine.mapper.AdministrativeOfficeMapper;
import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import com.oyun.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.medicine.service.HospitalizedCaseService;
import com.oyun.modules.medicine.domain.vo.HospitalizedCaseQueryCriteria;
import com.oyun.modules.medicine.mapper.HospitalizedCaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author 呼和
* @date 2024-07-02
**/
@Service
@RequiredArgsConstructor
public class HospitalizedCaseServiceImpl extends ServiceImpl<HospitalizedCaseMapper, HospitalizedCase> implements HospitalizedCaseService {

    private final HospitalizedCaseMapper hospitalizedCaseMapper;

    private final AdministrativeOfficeMapper administrativeOfficeMapper;
    @Override
    public PageResult<HospitalizedCase> queryAll(HospitalizedCaseQueryCriteria criteria, Page<Object> page){
        if (StringUtils.isNotEmpty(criteria.getName())) {
            criteria.setReplaceAll(IntermediateCodeUtils.mn2Code(criteria.getName()).replaceAll("-", " -"));
        }
        IPage<HospitalizedCase> all = hospitalizedCaseMapper.findAll(criteria, page);
        for (HospitalizedCase record : all.getRecords()) {
            LambdaQueryWrapper<AdministrativeOffice> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AdministrativeOffice::getId,record.getAdministrativeId());
            AdministrativeOffice administrativeOffice = administrativeOfficeMapper.selectOne(wrapper);
            record.setAdministrativeName(administrativeOffice.getMnName());
        }
        return PageUtil.toPage(all);
    }

    @Override
    public List<HospitalizedCase> queryAll(HospitalizedCaseQueryCriteria criteria){
        return hospitalizedCaseMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(HospitalizedCase resources) {
        convertCode(resources);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(HospitalizedCase resources) {
        HospitalizedCase hospitalizedCase = getById(resources.getId());
        hospitalizedCase.copy(resources);
        convertCode(hospitalizedCase);
        saveOrUpdate(hospitalizedCase);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<HospitalizedCase> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HospitalizedCase hospitalizedCase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("科室", hospitalizedCase.getAdministrativeId());
            map.put("名称", hospitalizedCase.getMnName());
            map.put(" mnNameCode",  hospitalizedCase.getMnNameCode());
            map.put(" zhName",  hospitalizedCase.getZhName());
            map.put("蒙语主诉", hospitalizedCase.getMnChief());
            map.put(" mnChiefCode",  hospitalizedCase.getMnChiefCode());
            map.put(" zhChief",  hospitalizedCase.getZhChief());
            map.put("蒙语现病史", hospitalizedCase.getMnPresentIllness());
            map.put(" zhPresentIllness",  hospitalizedCase.getZhPresentIllness());
            map.put(" mnPresentIllnessCode",  hospitalizedCase.getMnPresentIllnessCode());
            map.put("蒙语脉诊", hospitalizedCase.getMnPulseTaking());
            map.put(" zhPulseTaking",  hospitalizedCase.getZhPulseTaking());
            map.put(" mnPulseTakingCode",  hospitalizedCase.getMnPulseTakingCode());
            map.put("舌诊", hospitalizedCase.getMnTongue());
            map.put(" mnTongueCode",  hospitalizedCase.getMnTongueCode());
            map.put(" zhTongue",  hospitalizedCase.getZhTongue());
            map.put("尿诊", hospitalizedCase.getMnUroscopy());
            map.put(" mnUroscopyCode",  hospitalizedCase.getMnUroscopyCode());
            map.put(" zhUroscopy",  hospitalizedCase.getZhUroscopy());
            map.put("辩证", hospitalizedCase.getMnDialectic());
            map.put(" zhDialectic",  hospitalizedCase.getZhDialectic());
            map.put(" mnDialecticCode",  hospitalizedCase.getMnDialecticCode());
            map.put("辅助检查", hospitalizedCase.getMnInspection());
            map.put(" mnInspectionCode",  hospitalizedCase.getMnInspectionCode());
            map.put(" zhInspection",  hospitalizedCase.getZhInspection());
            map.put("蒙语蒙医诊断", hospitalizedCase.getMnDiagnosis());
            map.put(" mnDiagnosisCode",  hospitalizedCase.getMnDiagnosisCode());
            map.put(" zhDiagnosis",  hospitalizedCase.getZhDiagnosis());
            map.put("蒙语西医诊断", hospitalizedCase.getMnWtDiagnosis());
            map.put(" mnWtDiagnosisCode",  hospitalizedCase.getMnWtDiagnosisCode());
            map.put(" zhWtDiagnosis",  hospitalizedCase.getZhWtDiagnosis());
            map.put("蒙语治疗原则", hospitalizedCase.getMnTptPrinciple());
            map.put(" mnTptPrincipleCode",  hospitalizedCase.getMnTptPrincipleCode());
            map.put(" zhTptPrinciple",  hospitalizedCase.getZhTptPrinciple());
            map.put("蒙语药物疗法", hospitalizedCase.getMnYwTherapy());
            map.put(" mnYwTherapyCode",  hospitalizedCase.getMnYwTherapyCode());
            map.put(" zhYwTherapy",  hospitalizedCase.getZhYwTherapy());
            map.put("蒙语饮食疗法", hospitalizedCase.getMnYsTherapy());
            map.put(" mnYsTherapyCode",  hospitalizedCase.getMnYsTherapyCode());
            map.put(" zhYsTherapy",  hospitalizedCase.getZhYsTherapy());
            map.put("疗术疗法", hospitalizedCase.getMnLsTherapy());
            map.put(" mnLsTherapyCode",  hospitalizedCase.getMnLsTherapyCode());
            map.put(" zhLsTherapy",  hospitalizedCase.getZhLsTherapy());
            map.put("起居疗法", hospitalizedCase.getMnQjTherapy());
            map.put(" mnQjTherapyCode",  hospitalizedCase.getMnQjTherapyCode());
            map.put(" zhQjTherapy",  hospitalizedCase.getZhQjTherapy());
            map.put("蒙语职业", hospitalizedCase.getMnJob());
            map.put(" mnJobCode",  hospitalizedCase.getMnJobCode());
            map.put(" zhJob",  hospitalizedCase.getZhJob());
            map.put("蒙语家庭历史", hospitalizedCase.getMnJtHistory());
            map.put(" mnJtHistoryCode",  hospitalizedCase.getMnJtHistoryCode());
            map.put(" zhJtHistory",  hospitalizedCase.getZhJtHistory());
            map.put("籍贯", hospitalizedCase.getMnNativePlace());
            map.put(" mnNativePlaceCode",  hospitalizedCase.getMnNativePlaceCode());
            map.put(" zhNativePlace",  hospitalizedCase.getZhNativePlace());
            map.put("发病病例", hospitalizedCase.getMnPathogenesis());
            map.put(" mnPathogenesisCode",  hospitalizedCase.getMnPathogenesisCode());
            map.put(" zhPathogenesis",  hospitalizedCase.getZhPathogenesis());
            map.put("婚育月经史", hospitalizedCase.getMnMarriageChildbirth());
            map.put(" mnMarriageChildbirthCode",  hospitalizedCase.getMnMarriageChildbirthCode());
            map.put(" zhMarriageChildbirth",  hospitalizedCase.getZhMarriageChildbirth());
            map.put("望诊", hospitalizedCase.getMnObservation());
            map.put(" mnObservationCode",  hospitalizedCase.getMnObservationCode());
            map.put(" zhObservation",  hospitalizedCase.getZhObservation());
            map.put("一般情况", hospitalizedCase.getMnCondition());
            map.put(" zhCondition",  hospitalizedCase.getZhCondition());
            map.put("年龄", hospitalizedCase.getAge());
            map.put("便诊", hospitalizedCase.getMnStoolDiagnosis());
            map.put(" zhStoolDiagnosis",  hospitalizedCase.getZhStoolDiagnosis());
            map.put(" mnStoolDiagnosisCode",  hospitalizedCase.getMnStoolDiagnosisCode());
            map.put("查体", hospitalizedCase.getMnPhysicalExamination());
            map.put(" zhPhysicalExamination",  hospitalizedCase.getZhPhysicalExamination());
            map.put("过敏史", hospitalizedCase.getMnAllergic());
            map.put(" mnAllergicCode",  hospitalizedCase.getMnAllergicCode());
            map.put(" zhAllergic",  hospitalizedCase.getZhAllergic());
            map.put("护理与预防", hospitalizedCase.getMnCarePrevention());
            map.put(" mnCarePreventionCode",  hospitalizedCase.getMnCarePreventionCode());
            map.put(" zhCarePrevention",  hospitalizedCase.getZhCarePrevention());
            map.put("婚否(字典)", hospitalizedCase.getMaritalStatus());
            map.put("治疗方案", hospitalizedCase.getMnTreatmentPlan());
            map.put(" mnTreatmentPlanCode",  hospitalizedCase.getMnTreatmentPlanCode());
            map.put(" zhTreatmentPlan",  hospitalizedCase.getZhTreatmentPlan());
            map.put("工作单位", hospitalizedCase.getMnWork());
            map.put(" mnWorkCode",  hospitalizedCase.getMnWorkCode());
            map.put(" zhWork",  hospitalizedCase.getZhWork());
            map.put("即往史", hospitalizedCase.getMnPreHistory());
            map.put(" mnPreHistoryCode",  hospitalizedCase.getMnPreHistoryCode());
            map.put(" zhPreHistory",  hospitalizedCase.getZhPreHistory());
            map.put("民族(字典)", hospitalizedCase.getNation());
            map.put("性别(字典)", hospitalizedCase.getSex());
            map.put("可靠性", hospitalizedCase.getMnReliability());
            map.put(" mnReliabilityCode",  hospitalizedCase.getMnReliabilityCode());
            map.put(" zhReliability",  hospitalizedCase.getZhReliability());
            map.put("个人史", hospitalizedCase.getMnPslHistory());
            map.put(" mnPslHistoryCode",  hospitalizedCase.getMnPslHistoryCode());
            map.put(" zhPslHistory",  hospitalizedCase.getZhPslHistory());
            map.put("主诉人", hospitalizedCase.getMnChiefSuitor());
            map.put(" zhChiefSuitor",  hospitalizedCase.getZhChiefSuitor());
            map.put(" mnChiefSuitorCode",  hospitalizedCase.getMnChiefSuitorCode());
            map.put("季节", hospitalizedCase.getSeason());
            map.put("入院时间", hospitalizedCase.getAdmissionTime());
            map.put("主治医师", hospitalizedCase.getMnPhysician());
            map.put(" mnPhysicianCode",  hospitalizedCase.getMnPhysicianCode());
            map.put(" zhPhysician",  hospitalizedCase.getZhPhysician());
            map.put("现状", hospitalizedCase.getMnStatusQuo());
            map.put(" mnStatusQuoCode",  hospitalizedCase.getMnStatusQuoCode());
            map.put(" zhStatusQuo",  hospitalizedCase.getZhStatusQuo());
            map.put("首程病例记录", hospitalizedCase.getMnFirstMedicalRecord());
            map.put(" zhFirstMedicalRecord",  hospitalizedCase.getZhFirstMedicalRecord());
            map.put("首程病例时间", hospitalizedCase.getFirstMedicalTime());
            map.put("医嘱", hospitalizedCase.getMnDoctorAdvice());
            map.put(" mnDoctorAdviceCode",  hospitalizedCase.getMnDoctorAdviceCode());
            map.put(" zhDoctorAdvice",  hospitalizedCase.getZhDoctorAdvice());
            map.put("病历", hospitalizedCase.getMnMedicalHistory());
            map.put(" mnMedicalHistoryCode",  hospitalizedCase.getMnMedicalHistoryCode());
            map.put(" zhMedicalHistory",  hospitalizedCase.getZhMedicalHistory());
            map.put(" createBy",  hospitalizedCase.getCreateBy());
            map.put(" createTime",  hospitalizedCase.getCreateTime());
            map.put(" updateBy",  hospitalizedCase.getUpdateBy());
            map.put(" updateTime",  hospitalizedCase.getUpdateTime());
            map.put("状态", hospitalizedCase.getStatus());
            map.put(" remark",  hospitalizedCase.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
    private void convertCode(HospitalizedCase hospitalizedCase) {
        hospitalizedCase.setMnNameCode(IntermediateCodeUtils.mn2Code(hospitalizedCase.getMnName()).replaceAll("-", " -"));
    }
}
